// pages/voteResults/voteResults.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiveDic:null,
    page: 1,
    dataArr: [],
    canUplod: null,//是否可以上拉加载
    voteMaxCount:0,//最大票数
    searchText:'',
    url:'',
    dic:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("接收到的参数是obj=" + options.receiveDic);//此处打印出来的仅仅是字符串 需要解析，解析如下
    this.setData({
      receiveDic: JSON.parse(options.receiveDic)
    })
    this.setData({
      url: util.VoteForWork + '?match_kge_id=' + this.data.receiveDic.megagame_id + '&src_action_flag=' + this.data.receiveDic.src_action_flag 
    })
    this.getDetailLists();
  },
  //点击查看详情
  viewFilmDetail: function (e) {
    var idStr = String(e.currentTarget.dataset.id);
    var titleStr = String(e.currentTarget.dataset.title);
    console.log('查看作品' + idStr);
    wx.navigateTo({
      url: `../knowledge/knowledge?kgeId=${idStr}&title=${titleStr}`
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    console.log('下拉刷新')
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      page: 1
    });
    this.getDetailLists();
  },

  //上拉加载
  onReachBottom: function () {
    console.log('上拉加载');
    if (this.data.canUplod) {
      this.getDetailLists();
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },
  search: function (event) {
    console.log('搜索关键字是' + event.detail.value);
    this.setData({
      page: 1,
      searchText: event.detail.value,
    });
    this.getDetailLists();
  },

  //获取投票结果列表
  getDetailLists: function () {
    var that = this;
    var dic = {
      page: that.data.page,
      page_size_count: 12,
      key_words: that.data.searchText
    }
    util.publicRequestWithToken(
      that.data.url,
      'GET',
      dic,
      //sucess
      function (data) {
          //成功
          if (that.data.page === 1) {
            //清空数据
            that.setData({
              dataArr: []
            })
          }
          var newArr = that.data.dataArr.concat(data.detail.results)
          if (data.detail.next) {
            that.setData({
              canUplod:true,
              page: that.data.page + 1,
              dataArr: newArr
            })
          } else {
            that.setData({
              canUplod:false,
              page: that.data.page,
              dataArr: newArr
            })
          }
          //拿到最大票数voteMaxCount
          if (that.data.dataArr.length > 0){
            that.setData({
              voteMaxCount: that.data.dataArr[0].vote_number
            })
          }else{
            that.setData({
              voteMaxCount: 0
            })
          }
         
          console.log('最大票数为' + that.data.voteMaxCount)
      },
      //fail
      function (error) { },
      function () {
        // complete
        wx.hideLoading()
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();

      }
    );
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})